<?php
/**
	素材详情页的下载 充值 按钮
**/
require_once(dirname(__FILE__)."/../include/common.inc.php");
require_once(DEDEINC."/memberlogin.class.php");

$ml = new MemberLogin();
$muid = $ml->M_ID;

$jbmc = "金币";//虚拟币名称等于金币
//获得频道模型ID
if($aid > 0)
{
	$row = $dsql->GetOne("SELECT ch.addtable,arc.mid FROM `#@__arctiny` arc LEFT JOIN `#@__channeltype` ch ON ch.id=arc.channel WHERE arc.id='$aid' ");
    $maintable = empty($row['addtable'])? '' : $row['addtable'];

}


/******************************************下载 dows******************************************/

$dowurl = "$cfg_phpurl/download.php?open=0&aid=$aid";//转换获取下载地址

$arc = $dsql->GetOne("select * from `{$maintable}` where aid='$aid'");//查询模板所需要的金币
$needmoney = $arc['needmoney'];
$zhneedmoney = $needmoney;

$member = $dsql->GetOne("select * from #@__member where mid='$muid'");//查询会员所拥有的金币
$money = $member['money'];
$memberup = $member['uptime']+$member['exptime']*3600*24;
$mrank = $member['rank'];
$time=time();
$at = $dsql->GetOne("select * from #@__archives where id='$aid'");//查询主表

//会员级别
$rank = $dsql->GetOne("select * from #@__arcrank where rank='$mrank'");//查询主表


//下载后剩余金币
$dmoney = $money - $needmoney;



//收藏按钮

$s = $dsql->GetOne("select * from #@__member_stow where aid='$aid' and mid='$ml->M_ID'");
if(($aid==$s['aid'])&& ($muid!=0))
{
   echo "document.getElementById('stows').innerHTML=\"<a onclick='cancel()' class='greenbot'><i class='iconfont icon-shoucang'></i>已收藏</a><input type='hidden' name='id' value='$aid' />\";";
}
elseif($muid!=0)
{
	echo "document.getElementById('stows').innerHTML=\"<a onclick='stows()' class='greenbot'><i class='iconfont icon-shoucang'></i>收藏</a><input type='hidden' name='id' value='$aid' />\";";
}
else
{
	echo "document.getElementById('stows').innerHTML=\"<a data-pop='liuyan' class='greenbot'><i class='iconfont icon-shoucang'></i>收藏</a>\";";
}


//如果会员已经登录
if($muid!=0)
{

	
	//如果是管理员
	if($ml->isAdmin==1)
	{
		echo "document.getElementById('dmsgtitle').innerHTML=\"下载".$at['title']."\";";
		echo "document.getElementById('dowmsg').innerHTML=\"<p><span class='float_l'>您是管理员，享免费特权</span></p>\";";
		echo "document.getElementById('popsub').innerHTML=\"<div class='pop-sub'><a id='dow' href='".$dowurl."' target='_blank' class='popup-ok'>免费下载</a><a class='popup-close'>取消</a></div>\";";
	}

	//如果是该资源的作者
	else if($wid==$muid)
	{
		echo "document.getElementById('dmsgtitle').innerHTML=\"下载".$at['title']."\";";
		echo "document.getElementById('dowmsg').innerHTML=\"<p><span class='float_l'>您是该资源的作者，享免费特权</span></p>\";";
		echo "document.getElementById('popsub').innerHTML=\"<div class='pop-sub'><a id='dow' href='".$dowurl."' target='_blank' class='popup-ok'>免费下载</a><a class='popup-close'>取消</a></div>\";";
	}

	//如果是免费素材
	else if($needmoney==0)
	{
		if($arc['daccess']>10)
		{
			//金币为0但当前会员级别小于所设置的下载会员级别时
			if(($memberup < $time) || ($mrank < $arc['daccess']))
			{
				echo "document.getElementById('dmsgtitle').innerHTML=\"下载".$at['title']."\";";
				echo "document.getElementById('dowmsg').innerHTML=\"<p><span class='float_l'>普通会员无下载权限，升级VIP会员可任意下载</span></p>\";";
				echo "document.getElementById('popsub').innerHTML=\"<div class='pop-sub'><a id='dow' href='".$cfg_memberurl."/buy.php#form_pay2' target='_blank' class='popup-ok'>开通VIP</a><a class='popup-close'>取消</a></div>\";";
			}
			else
			{
				echo "document.getElementById('dmsgtitle').innerHTML=\"下载".$at['title']."\";";
				echo "document.getElementById('dowmsg').innerHTML=\"<p><span class='float_l'>您是".$rank['membername']."，该素材享免费特权</span></p>\";";
				echo "document.getElementById('popsub').innerHTML=\"<div class='pop-sub'><a id='dow' href='".$dowurl."' target='_blank' class='popup-ok'>免费下载</a><a class='popup-close'>取消</a></div>\";";
			}
		}
		else
		{
			echo "document.getElementById('dmsgtitle').innerHTML=\"下载".$at['title']."\";";
			echo "document.getElementById('dowmsg').innerHTML=\"<p><span class='float_l'>该素材为免费资源，请下载</span></p>\";";
			echo "document.getElementById('popsub').innerHTML=\"<div class='pop-sub'><a id='dow' href='".$dowurl."' target='_blank' class='popup-ok'>免费下载</a><a class='popup-close'>取消</a></div>\";";
		}
	}

	//VIP会员特定素材免费下载
	else if(($memberup >= $time) && ($mrank >= $arc['daccess']) && ($arc['daccess'] > 10))
	{
		echo "document.getElementById('dmsgtitle').innerHTML=\"下载".$at['title']."\";";
		echo "document.getElementById('dowmsg').innerHTML=\"<p><span class='float_l'>您是".$rank['membername']."，该素材享免费特权</span></p>\";";
		echo "document.getElementById('popsub').innerHTML=\"<div class='pop-sub'><a id='dow' href='".$dowurl."' target='_blank' class='popup-ok'>免费下载</a><a class='popup-close'>取消</a></div>\";";
	}

	//正常下载扣费判断流程
	else
	{
		if($mrank>10){
			$d = $dsql->GetOne("select mid from #@__member_operation where arcid='".$aid."' And mid='$muid'");
		}else{ 
			$d = $dsql->GetOne("select mid,vip from #@__member_operation where vip=0 and arcid='".$aid."' And mid='$muid'"); 
		}
		
		//如果没有下载过
		if($d['mid'] != $muid)
		{
			//如果金币不足 显示充值按钮
			if($money < $needmoney)
			{
				echo "document.getElementById('dowmsg').innerHTML=\"<p><span class='float_l'>您的余额</span><span class='float_r'>".$money."<i>$jbmc</i></span></p><p><span class='float_l'>下载需要</span><span class='float_r'><b>".$needmoney."</b><i>$jbmc</i></span></p><p><span class='float_l'>下载后余额</span><span class='float_r'>余额不足，请先充值！</span></p>\";";
				echo "document.getElementById('popsub').innerHTML=\"<div class='pop-sub'><a id='dow' href='".$cfg_memberurl."/buy.php' class='popup-ok' target='_blank'>在线充值</a></div>\";";
			}
			//金币足够本次下载 直接显示下载
			else
			{
				echo "document.getElementById('dowmsg').innerHTML=\"<p><span class='float_l'>您的余额</span><span class='float_r'>".$money."<i>$jbmc</i></span></p><p><span class='float_l'>下载需要</span><span class='float_r'><b>".$needmoney."</b><i>$jbmc</i></span></p><p><span class='float_l'>下载后余额</span><span class='float_r'>$dmoney<i>$jbmc</i></span></p>\";";
				echo "document.getElementById('popsub').innerHTML=\"<div class='pop-sub'><a id='dow' href='".$dowurl."' target='_blank' class='popup-ok'>确认支付</a><a class='popup-close'>取消</a></div>\";";
			}
		}
		
		//如果有下载过 则提示不重复扣费
		else
		{
			echo "document.getElementById('dowmsg').innerHTML=\"<p><span class='float_l'>您下载过该素材，多次下载不再扣费</span></p>\";";
			echo "document.getElementById('popsub').innerHTML=\"<div class='pop-sub'><a id='dow' href='".$dowurl."' target='_blank' class='popup-ok'>免费下载</a><a class='popup-close'>取消</a></div>\";";
		}
	}
	
	echo "document.getElementById('dmsgtitle').innerHTML=\"下载".$at['title']."\";";
}
//如果没有登录 点击登录按钮弹出请登录提示
else
{
	echo "document.getElementById('dmsgtitle').innerHTML=\"请登录后下载\";";
	echo "document.getElementById('dowmsg').innerHTML=\"<form name='userlogin' onsubmit='return Dologin();' method='POST'><div class='login'><input type='hidden' name='fmdo' value='ajaxlogin' /><input type='hidden' name='dopost' value='ajaxlogin' /><input type='hidden' name='keeptime' value='604800' /><ul class='arc-loginlist'><li class='uname'><input type='text' name='userid' class='input_n' placeholder='请输入用户名' /><a href='".$cfg_memberurl."/reg_new.php' target='_blank'>注册新用户</a></li><li class='pwd'><input type='password' name='pwd' class='input_o' placeholder='请输入密码' /><a id='dow' href='".$cfg_memberurl."/resetpassword.php' target='_blank'>忘记密码?</a></li></ul></div><div class='pop-sub'><button type='submit' class='popup-ok arc-loginBtn'>登录</button></div><div class='arc-login-hzzh'><p>使用合作帐号登录</p><a href='".$cfg_cmsurl."/qqLogin/' class='login-qq' target='_blank'><img src='".$cfg_cmsurl."/images/qq_login.png' style='width:120px;' /></a></div></form>\";";
}

$arc = $dsql->GetOne("SELECT title FROM `#@__archives` WHERE id='$aid'");

$mmm = $dsql->GetOne("select userid from #@__member where uname='$w'");

/**管理员给用户发消息***/
if($ml->isAdmin==1)
{
	echo "document.getElementById('twuser').innerHTML=\"<a href='$cfg_memberurl/?uid=".$mmm['userid']."' target='_blank'>TA的空间</a><a data-pop='tor'>发消息</a>\";";
	echo "document.getElementById('towriter').innerHTML=\"<form method='post' action='".$cfg_cmsurl."/i/fankui.php' name='form1'><input type='hidden' name='type' value='wliuyan' /><input type='hidden' name='wid' value='".$wid."' /><input type='hidden' name='arctitle' value='".$arc['title']."' /><div class='login liuyan'><p>请填写您要发送给".$w."的消息内容。</p><ul class='arc-loginlist'><li class='pwd'><input type='text' name='title' class='input' placeholder='标题' required='required' style='width:295px;' value='".$arc['title']."' /></li><li class='uname'><textarea name='wneir' class='srk' placeholder='内容' required='required'></textarea></li></ul></div><div class='pop-sub'><button type='submit' class='popup-ok arc-loginBtn' id='dow'>提交</button></div></form>\";";
}
/**管理员给用户发消息结束**/
?>

